﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Linq;
using System.Globalization;

namespace GenerateDictionary
{
    /// <summary>
    /// Interaction logic for CharsetControl.xaml
    /// </summary>
    public partial class CharsetControl : UserControl, IDisposable
    {
        public DataTable dt = new DataTable();
        public CultureInfo standard = CultureInfo.InvariantCulture;
        public delegate void UserChangedHandler(object sender, CharactureSetControlUserChangedEventArgs e);
        public event UserChangedHandler OnUserChanged;

        public CharsetControl()
        {
            InitializeComponent();
            setupCharsetDataTable();

            
            CharvalsLBOX.DataContext = dt;
            
            
        }

        private void setupCharsetDataTable()
        {
            dt.Clear();
            DataColumn valueandname = new DataColumn("ASCIIValueandVal", typeof(string));
            DataColumn ischecked = new DataColumn("IsSelected", typeof(bool));
            DataColumn intcharvalues = new DataColumn("INTCHARVALS", typeof(int));
            dt.Columns.Add(valueandname);
            dt.Columns.Add(ischecked);
            dt.Columns.Add(intcharvalues);

            


            
            var charmin = 0;
            while (charmin <= 127)
            {
                dt.Rows.Add(new object[] { "ASCII DEC.( " + charmin.ToString(standard) + " ) VALUE ([  " + Utility.Utility.getnicecharvalueonint(charmin) + "  ])", false, charmin });
                charmin++;
            }
            setinitialselectedelements();
            //clean up
            valueandname.Dispose();
            ischecked.Dispose();
            intcharvalues.Dispose();

        }

        private void setinitialselectedelements()
        {
            for (int x = 0; x < dt.Rows.Count; x++)
            {
                if ((int)dt.Rows[x]["INTCHARVALS"] > 47 && (int)dt.Rows[x]["INTCHARVALS"] < 58)
                {
                    dt.Rows[x]["IsSelected"] = true;
                }
                if ((int)dt.Rows[x]["INTCHARVALS"] > 64 && (int)dt.Rows[x]["INTCHARVALS"] < 91)
                {
                    dt.Rows[x]["IsSelected"] = true;
                }
                if ((int)dt.Rows[x]["INTCHARVALS"] > 96 && (int)dt.Rows[x]["INTCHARVALS"] < 123)
                {
                    dt.Rows[x]["IsSelected"] = true;
                }
            }
        }

       

            public void clearselections(){
                for(int x = 0;x<dt.Rows.Count;x++){
                  
                    dt.Rows[x]["IsSelected"] = false;
              }

            }
            public void setnumbersSelected(){
                for(int x = 0;x<dt.Rows.Count;x++){
                    if((int)dt.Rows[x]["INTCHARVALS"] > 47 && (int)dt.Rows[x]["INTCHARVALS"] <58){
                        dt.Rows[x]["IsSelected"] = true;
                    }

                }
               
            }

            public void setUPPERSSelected(){
                for(int x = 0;x<dt.Rows.Count;x++){
                    if((int)dt.Rows[x]["INTCHARVALS"] > 64 && (int)dt.Rows[x]["INTCHARVALS"] <91){
                        dt.Rows[x]["IsSelected"] = true;
                    }

                }
               
            }
        public void setLOWERSSelected(){
                for(int x = 0;x<dt.Rows.Count;x++){
                    if((int)dt.Rows[x]["INTCHARVALS"] > 96 && (int)dt.Rows[x]["INTCHARVALS"] <123){
                        dt.Rows[x]["IsSelected"] = true;
                    }

                }
               
            }

        public IEnumerable<char> getSelectedcharset(){
            foreach (var r in dt.Select("IsSelected = true"))
            {
                yield return  (char)((int)r["INTCHARVALS"]);
                
            }

            }

        private void elementclicked(object sender, RoutedEventArgs e)
        {
            // Make sure someone is listening to event
            if (OnUserChanged == null) return;

            CharactureSetControlUserChangedEventArgs args = new CharactureSetControlUserChangedEventArgs((sender as CheckBox));
            OnUserChanged(this, args);
        }




        internal void setselectedchars(List<char> list)
        {
            clearselections();
            foreach(char el in list){
                for (int x = 0; x < dt.Rows.Count; x++)
                {
                    if ((char)((int)dt.Rows[x]["INTCHARVALS"]) == el)
                    {
                        dt.Rows[x]["IsSelected"] = true;
                    }

                }

            }
        }

        public void Dispose(){
            Dispose(false);
            


        }

        private void Dispose(bool p)
        {
            if (p)
            {
                dt.Dispose();
                GC.SuppressFinalize(this);
            }
            else
            {
                dt.Dispose();
            }
        }
    }
    

public class CharactureSetControlUserChangedEventArgs : EventArgs
{
    public string info { get; private set; }

    public CharactureSetControlUserChangedEventArgs(CheckBox itemclicked)
    {

        info = (string)itemclicked.Content;
    }



}

}